home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / faq / vol15n14.zip / DDGAME.ZIP / ANIM.CPP next >
C/C++ Source or Header  |  1996-04-24  |  6KB  |  208 lines

  1. // animation.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "anim.h"
  6.  
  7. #include "error.h"
  8.  
  9. int CAnim::DisplayWidth = 640;  // defaults to 640 if not set
  10.  
  11. //-----------------------------------------------------------------------------
  12. // SetDisplayWidth
  13. //-----------------------------------------------------------------------------
  14. void CAnim::SetDisplayWidth(int nDisplayWidth)
  15. {
  16.   CAnim::DisplayWidth = nDisplayWidth;
  17. }
  18.  
  19. //-----------------------------------------------------------------------------
  20. // constructor
  21. //-----------------------------------------------------------------------------
  22. CAnim::CAnim(LPDIRECTDRAW pDirectDraw,
  23.              char* pFileRoot,
  24.              char* pDirectoryName,
  25.              int nNumFrames,
  26.              BOOL bStoreInVideo) // = TRUE
  27. {
  28.   m_pDirectDraw = pDirectDraw;
  29.   m_nNumFrames = nNumFrames;
  30.   m_bInVideo = bStoreInVideo;
  31.  
  32.   // create new string 5 characters longer that dir name: 4 for
  33.   // file name, and 1 for terminating NULL
  34.   m_pFileRoot = new char[strlen(pDirectoryName) + 5];
  35.   strcpy(m_pFileRoot, pDirectoryName);
  36.   strncat(m_pFileRoot, pFileRoot, 4);
  37.  
  38.   m_pnRows = new int[nNumFrames];
  39.   m_pnCols = new int[nNumFrames];
  40.  
  41.   m_pSurface = NULL;
  42. }
  43.  
  44. //-----------------------------------------------------------------------------
  45. // destructor
  46. //-----------------------------------------------------------------------------
  47. CAnim::~CAnim()
  48. {
  49.   delete [] m_pFileRoot;
  50.   delete [] m_pnRows;
  51.   delete [] m_pnCols;
  52. }
  53.                                
  54. //-----------------------------------------------------------------------------
  55. // Load
  56. //-----------------------------------------------------------------------------
  57. BOOL CAnim::Load()
  58. {
  59.   return InternalLoad(FALSE);
  60. }
  61.  
  62. //-----------------------------------------------------------------------------
  63. // Release
  64. //-----------------------------------------------------------------------------
  65. void CAnim::Release()
  66. {
  67.   m_pSurface->Release();
  68. }
  69.  
  70. //-----------------------------------------------------------------------------
  71. // Restore
  72. //-----------------------------------------------------------------------------
  73. BOOL CAnim::Restore()
  74. {
  75.   m_pSurface->Restore();
  76.  
  77.   return InternalLoad(TRUE);
  78. }
  79.  
  80. //-----------------------------------------------------------------------------
  81. // Render
  82. //-----------------------------------------------------------------------------
  83. HRESULT CAnim::Render(int nDestX,
  84.                       int nDestY,
  85.                       int nFrameNum,
  86.                       LPDIRECTDRAWSURFACE pDestSurface,
  87.                       BOOL bTransparent) // = TRUE
  88. {
  89.   HRESULT hr;
  90.   CRect srcRect(m_pnCols[nFrameNum], m_pnRows[nFrameNum],
  91.                 m_pnCols[nFrameNum] + m_nWidth, m_pnRows[nFrameNum] + m_nHeight);
  92.  
  93.   if (bTransparent)
  94.     hr = pDestSurface->BltFast(nDestX, nDestY, m_pSurface, &srcRect,
  95.                                DDBLTFAST_SRCCOLORKEY);
  96.   else
  97.     hr = pDestSurface->BltFast(nDestX, nDestY, m_pSurface, &srcRect,
  98.                                DDBLTFAST_NOCOLORKEY);
  99.  
  100.   #ifdef _DEBUG
  101.   if (hr != DD_OK)
  102.     DDrawError("CAnim::Render BltFast::", hr);
  103.   #endif
  104.   
  105.   return hr;
  106. }
  107.  
  108. //-----------------------------------------------------------------------------
  109. // InternalLoad
  110. //-----------------------------------------------------------------------------
  111. BOOL CAnim::InternalLoad(BOOL bRestore)
  112. {
  113.   HBITMAP hBitmap;
  114.   BITMAP bitmap;
  115.   DDSURFACEDESC ddSurfDesc;
  116.   HRESULT hResult;
  117.   HDC hdcSource;
  118.   HDC hdcDest;
  119.  
  120.   char* pFileName = new char[strlen(m_pFileRoot) + 9];
  121.  
  122.   for (int nIndex = 0; nIndex < m_nNumFrames; nIndex++)
  123.   {
  124.     wsprintf(pFileName, "%s%04d.bmp", m_pFileRoot, nIndex);
  125.  
  126.     // load bitmap
  127.     hBitmap = (HBITMAP)LoadImage(NULL, pFileName, IMAGE_BITMAP,
  128.                                  0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
  129.     if (!hBitmap)
  130.     {
  131.       if (m_pSurface)
  132.         m_pSurface->Release();
  133.       delete [] pFileName;
  134.       return FALSE;
  135.     }
  136.  
  137.     // create the dd surface the first time through
  138.     if (0 == nIndex && !bRestore)
  139.     {
  140.       GetObject(hBitmap, sizeof(bitmap), &bitmap);
  141.       m_nWidth = bitmap.bmWidth;
  142.       m_nHeight = bitmap.bmHeight;
  143.  
  144.       if (m_nNumFrames * m_nWidth > CAnim::DisplayWidth)
  145.       {
  146.         m_nNumCols = CAnim::DisplayWidth / m_nWidth;
  147.         m_nNumRows = int(float(m_nNumFrames) / float(m_nNumCols) + 1.0);
  148.       }
  149.       else
  150.       {
  151.         m_nNumCols = m_nNumFrames;
  152.         m_nNumRows = 1;
  153.       }
  154.  
  155.       ddSurfDesc.dwSize = sizeof(ddSurfDesc);
  156.       ddSurfDesc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
  157.       ddSurfDesc.dwWidth = m_nWidth * m_nNumCols;
  158.       ddSurfDesc.dwHeight = m_nHeight * m_nNumRows;
  159.  
  160.       if (m_bInVideo)
  161.         ddSurfDesc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
  162.       else
  163.         ddSurfDesc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
  164.  
  165.       hResult = m_pDirectDraw->CreateSurface(&ddSurfDesc, &m_pSurface, NULL);
  166.       
  167.       if (hResult != DD_OK)
  168.       {
  169.         DDrawError("CAnim::Load CreateSurface:", hResult);
  170.         delete [] pFileName;
  171.         return FALSE;
  172.       }
  173.       
  174.       DDCOLORKEY ddck;
  175.       ddck.dwColorSpaceLowValue = RGB(0,0,0);
  176.       ddck.dwColorSpaceHighValue = RGB(0,0,0);
  177.       m_pSurface->SetColorKey(DDCKEY_SRCBLT, &ddck);
  178.     }
  179.  
  180.     m_pnRows[nIndex] = nIndex / m_nNumCols * m_nHeight;
  181.     m_pnCols[nIndex] = nIndex % m_nNumCols * m_nWidth;
  182.  
  183.     hResult = m_pSurface->GetDC(&hdcDest);
  184.     if (DD_OK == hResult)
  185.     {
  186.       ASSERT(m_pSurface != NULL);
  187.       hdcSource = CreateCompatibleDC(hdcDest);
  188.       SelectObject(hdcSource, hBitmap);
  189.  
  190.       StretchBlt(hdcDest, 
  191.                  m_pnCols[nIndex], m_pnRows[nIndex],
  192.                  m_nWidth, m_nHeight,
  193.                  hdcSource,
  194.                  0, 0,
  195.                  m_nWidth, m_nHeight,
  196.                  SRCCOPY);
  197.       m_pSurface->ReleaseDC(hdcDest);
  198.     }
  199.  
  200.     DeleteDC(hdcSource);
  201.     DeleteObject(hBitmap);
  202.   }
  203.  
  204.   // everything seems ok
  205.   delete [] pFileName;
  206.   return TRUE;
  207. }
  208.